<template>
  <div>
    <el-tree
      :data="data"
      :props="defaultProps"
      :expand-on-click-node="false"
      show-checkbox
      node-key="catId"
      :default-expanded-keys="expandKey"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            Append
          </el-button>

          <el-button type="text" size="mini" @click="() => edit(data)">
            Edit
          </el-button>

          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog :title="dialogType" :visible.sync="dialogVisible" width="30%">
      <el-form :model="category">
        <el-form-item label="分类名称">
          <el-input v-model="category.name" type="text"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="category.icon" type="text"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input v-model="category.productUnit" type="text"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- <el-button @click="format1"></el-button> -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogType: "", //对话框复用，当使用修改时候赋值Edit，当添加时为add
      category: { showStatus: 1, sort: 0 },
      dialogVisible: false,
      data: [],
      expandKey: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  created() {
    this.getMenus();
  },
  methods: {
    // TODO 等会删除
    format1() {
      var format =
        '<p><img class="wscnph" src="" /></p>';
      format.replace(
        /<img/gi,
        '<img style="max-width:100%;height:auto;display:inline-block;margin:10px auto;"'
      );

      console.log(format);
    },
    // 用于判定修改还是删除
    submit() {
      if (this.dialogType == "add") {
        this.addCategory();
      }
      if (this.dialogType == "edit") {
        this.editData();
      }
    },
    // 修改数据
    editData() {
      var { catId, nameicon, icon, productUnit } = this.category;
      var data = { catId, nameicon, icon, productUnit };
      this.$http.update("/gulimall-product/product/category", data).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        this.dialogVisible = false;
        this.getMenus();
        this.expandKey = [this.category.parentCid];
      });
    },
    // 打开修改分类对话框，数据回显
    edit(data) {
      this.$http.get(`/gulimall-product/product/category/${data.catId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.category.name = res.data.name;
        this.category.catId = res.data.catId;
        this.category.icon = res.data.icon;
        this.category.productUnit = res.data.productUnit;
        this.category.parentCid = res.data.parentCid;
      });
      this.dialogType = "edit";
      this.dialogVisible = true;
    },
    // 添加分类
    addCategory() {
      this.$http
        .post("/gulimall-product/product/category", this.category)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.$message({
            type: "success",
            message: "添加成功!",
          });
          this.dialogVisible = false;
          this.getMenus();
          this.expandKey = [this.category.parentCid];
        })
        .catch(() => {});
    },
    getMenus() {
      this.$http
        .get("/gulimall-product/product/category/list/tree")
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          console.log("分类维护树", res);
          this.data = res.data;
        })
        .catch(() => {});
    },
    append(data) {
      this.category = {};
      this.dialogType = "add";
      this.dialogVisible = true;
      console.log("append", data);
      this.category.parentCid = data.catId;
      this.category.catLevel = data.catLevel * 1 + 1;
    },

    remove(node, data) {
      var ids = [data.catId];

      this.$confirm(`将删除【${data.name}】目录，是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: "/gulimall-product/product/category",
            method: "delete",
            data: ids,
          }).then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getMenus();
            // console.log("remove成功", node, data);
            this.expandKey = [node.parent.data.catId];
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style></style>
